package roborally.commandCombiners;
import be.kuleuven.cs.som.annotate.Basic;
import roborally.*;
import roborally.Conditions.*;
import roborally.Commands.*;
import roborally.conditionCombiners.ConditionCombiner;


/**
 * A class of While-commands for playing the roborally game, involving a condition and a body to be executed if the condition holds.
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public class While extends CommandCombiner {
	
	private final Statement condition;
	//condition must be a condition or a ConditionCombiner
	private Statement body;
	//body must be a command or a CommandCombiner
	
	/**
	 * Create a new while command with the given parameters.
	 * @param 	condition
	 * 			The condition for this while command.
	 * @param 	body
	 * 			The body of this while command that is executed when the condition is true.
	 * @pre		Body must be a command or a commandCombiner
	 */
	public While(Statement condition, Statement body){
		assert(condition instanceof Statement || condition instanceof ConditionCombiner);
		assert(body instanceof Command || body instanceof CommandCombiner);
		this.condition = condition;
		this.body = body;
		this.setStringForm("while ("+condition.getStringForm()+") do ("+body.getStringForm()+")");
	}
	
	/**
	 * Execute the body of this combinator if the condition is true.
	 * The body is either a command or a combination of commands.
	 */
	@Override
	public void execute(Robot robot) {
		assert(body instanceof CommandCombiner || body instanceof Command);
		if(condition.validate(robot)){
			body.execute(robot);
		}
				
	}

	/**
	 * Get the body of this while command.
	 */
	@Basic
	public Statement getBody() {
		return body;
	}

	/**
	 * Set the body of this while command.
	 */
	public void setBody(Statement body) {
		this.body = body;
	}

	/**
	 * Get the condition of this while command.
	 */
	@Basic
	public Statement getCondition() {
		return condition;
	}

}
